package com.jqsoft.launcher.activity.gairui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.R;
import com.garea.medical.MedicalFactory;
import com.garea.medical.spo2.ISpo2;
import com.garea.medical.spo2.ISpo2.OnSpo2Listener;
import com.garea.medical.spo2.ISpo2Data;
import com.garea.medical.spo2.ISpo2State;

/**
 * 测量血氧
 * @作者：周桃有
 * @日期：2015-7-8
 */
public class Spo2Example extends JQActivity {
	private ISpo2 mSpo2;
	private TextView tvState, tvSpo2, tvPr;
	private Button btnSave;
	private String cPulseRate="",cxueyang="";
	
	@Override
	protected int getContentLayout() {
		
		return R.layout.example_spo2;
	}

	@Override
	protected void initGui() {
		tvState = (TextView) this.findViewById(R.id.tvSpo2State);
		tvSpo2 = (TextView) this.findViewById(R.id.tvSpo2);
		tvPr = (TextView) this.findViewById(R.id.tvPr);
		btnSave=(Button) findViewById(R.id.btnSave);
		
	}

	@Override
	protected void initAction() {
		setGoBack();
		initSpo2();
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//设置当前activity被关闭的时候的结果数据
				Intent data = new Intent();
				data.putExtra("cPulseRate", cPulseRate);
				data.putExtra("cxueyang", cxueyang);
				setResult(4, data);
				//关闭当前的activity
				Spo2Example.this.finish();
			}
			
		});
	}

	@Override
	protected void initData() {
		setTitle("测量血氧脉率");
		
	}
	
	private void initSpo2() {
		mSpo2 = MedicalFactory.createLocalSpo2();
		mSpo2.setListener(new OnSpo2Listener() {

			@Override
			public void onReceivedData(ISpo2Data d) {
				//接收血氧数据
				if (d.isSpo2Valid()) {
					cxueyang=Integer.toString(d.getSpo2());
					tvSpo2.setText(cxueyang);
				} else {
					tvSpo2.setText("---");
				}
				
				if (d.isPrValid()) {
					cPulseRate=Integer.toString(d.getPr());
					tvPr.setText(cPulseRate);
				} else {
					tvPr.setText("---");
				}
			}

			@Override
			public void onReceivedState(ISpo2State s) {
				//接收血氧状态
				Log.i("Spo2Example", "State: " + s.getState());
				if (s.getState() == ISpo2State.GAREA_V2_SPO2_FINGER_OFF) {
					tvState.setText("手指脱落");
				} else if (s.getState() == ISpo2State.GAREA_V2_SPO2_SEARCHING) {
					tvState.setText("搜索脉搏");
				} else if (s.getState() == ISpo2State.GAREA_V2_SPO2_LONG_SEARCHING) {
					tvState.setText("搜索脉搏时间过长");
				} else {
					tvState.setText("测量中");
				}
			}
			
			@Override
			public void onInited() {
				mSpo2.start();
			}
		});
		mSpo2.init();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		mSpo2.stop();
		mSpo2.uninit();
	}
	
}
